﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

using SharpBase.Web;
using SharpOSCAR;
using SharpOSCAR.Flap;
using SharpOSCAR.Snac;
using SharpOSCAR.Snac.BUDDY;
using SharpOSCAR.Snac.FEEDBAG;
using SharpOSCAR.Snac.LOCATE;
using SharpOSCAR.Snac.OSERVICE;
using SharpOSCAR.Snac.PD;
using SharpSNS;
using SharpSNS.Auth;

namespace SharpSNSTest
{
    class Program
    {
        private static bool dataIn;
        private static StartOscarSessionResponse sosr;

        static void Main(string[] args)
        {
            
            Console.Write(BitConverter.IsLittleEndian);
            SharpSNS.Auth.ClientLoginResponse clr = SharpSNS.Auth.Auth.ClientLogin("sh1PFM_-rkulSyHF", "sharposcartest", "qweqwe");
            if (clr.ResponseCode == SharpSNS.ApiResponseCode.Success)
            {
                sosr = SharpSNS.Auth.Auth.StartOscarSession("sh1PFM_-rkulSyHF", clr);
                if (sosr.ResponseCode == ApiResponseCode.Success)
                {
                    Console.WriteLine("BOSS Server: {0}:{1}", sosr.BOSSHost, sosr.BOSSPort);
                    Console.WriteLine("BOSS Cookie: {0}", Convert.ToBase64String(sosr.BOSSCookie));
                    SimpleAsyncSocket sas = new SimpleAsyncSocket();
                    sas.Closed += new EventHandler(sas_Closed);
                    sas.DataReceived += new EventHandler<SimpleAsyncSocket.DataReceivedEventArgs>(sas_DataReceived);
                    sas.Connect(sosr.BOSSHost, sosr.BOSSPort);
                    //sas.SendData(Encoding.Default.GetBytes(Console.ReadLine()));
                    Console.ReadLine();

                    Console.WriteLine("Sending BRQ");
                    BUDDY__RIGHTS_QUERY brq = new BUDDY__RIGHTS_QUERY(201);
                    FLAP__DATA_FRAME brqflap = new FLAP__DATA_FRAME(101, brq);
                    sas.SendData(brqflap.ToByteArray());

                    Console.WriteLine("Sending PDRQ");
                    PD__RIGHTS_QUERY prq = new PD__RIGHTS_QUERY(202);
                    FLAP__DATA_FRAME prqflap = new FLAP__DATA_FRAME(102, prq);
                    sas.SendData(prqflap.ToByteArray());

                    Console.WriteLine("Sending LRQ");
                    LOCATE__RIGHTS_QUERY lrq = new LOCATE__RIGHTS_QUERY(203);
                    FLAP__DATA_FRAME lrqflap = new FLAP__DATA_FRAME(103, lrq);
                    sas.SendData(lrqflap.ToByteArray());

                    Console.WriteLine("Sending FRQ");
                    FEEDBAG__RIGHTS_QUERY frq = new FEEDBAG__RIGHTS_QUERY(204);
                    FLAP__DATA_FRAME frqflap = new FLAP__DATA_FRAME(104, frq);
                    sas.SendData(frqflap.ToByteArray());

                    Console.WriteLine("Sending FQ");
                    FEEDBAG__QUERY fq = new FEEDBAG__QUERY(205);
                    FLAP__DATA_FRAME fqflap = new FLAP__DATA_FRAME(105, fq);
                    sas.SendData(fqflap.ToByteArray());

                    Console.ReadLine();

                    Console.WriteLine("Sending FU");
                    FEEDBAG__USE fu = new FEEDBAG__USE(206);
                    FLAP__DATA_FRAME fuflap = new FLAP__DATA_FRAME(106, fu);
                    sas.SendData(fuflap.ToByteArray());

                    Console.ReadLine();

                    Console.WriteLine("Sending OSCO");
                    OSERVICE__CLIENT_ONLINE osco = new OSERVICE__CLIENT_ONLINE(207);
                    FLAP__DATA_FRAME oscoflap = new FLAP__DATA_FRAME(107, osco);
                    sas.SendData(oscoflap.ToByteArray());
                }
            }
            else
                Console.WriteLine("{0}\r\n{1}", clr.ResponseCode, clr.StatusDetailCode);
            Console.ReadLine();

        }

        static void sas_DataReceived(object sender, SimpleAsyncSocket.DataReceivedEventArgs e)
        {
            Console.Write("Data Received:   ");
            foreach (byte b in e.DataBuffer)
                Console.Write("{0},", b);
            Console.WriteLine();
            if (!dataIn)
            {
                SimpleAsyncSocket sas = sender as SimpleAsyncSocket;
                dataIn = true;
                FLAP__SIGNON_FRAME fsp = new FLAP__SIGNON_FRAME(100, sosr.BOSSCookie);
                byte[] outgoing = fsp.ToByteArray();
                Console.Write("Data Sent:       ");
                foreach (byte b in outgoing)
                    Console.Write("{0},", b);
                Console.WriteLine();
                sas.SendData(fsp.ToByteArray());

                //OSERVICE__CLIENT_ONLINE osco = new OSERVICE__CLIENT_ONLINE(200);
                //FlapDataPacket fdp = new FlapDataPacket(101, osco);
                //byte[] outgoing = fdp.ToByteArray();
                //Console.Write("Data Sent:       ");
                //foreach (byte b in outgoing)
                //    Console.Write("{0} ", b);
                //(sender as SimpleAsyncSocket).SendData(fdp.ToByteArray());
            }
        }

        static void sas_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("Socket closed.");
        }
    }
}